
#include "cubemap.h"
#include <assert.h>

void Cubemap_Init(const char* filename)
{
	if (Cubemap_textureId != 0)
		Cubemap_Destroy();
	Cubemap_textureId = Texture_LoadTexture(filename,TRUE);
	Cubemap_displayList = DisplayList_Create(Cubemap_s_Draw,(void*)&Cubemap_textureId);
	assert(Cubemap_textureId != 0);
	assert(Cubemap_displayList != 0);
}

void Cubemap_InitFromTextureId(GLuint textureId)
{
	if (Cubemap_textureId != 0)
		Cubemap_Destroy();
	Cubemap_textureId = textureId;
	Cubemap_displayList = DisplayList_Create(Cubemap_s_Draw,(void*)&textureId);
	assert(Cubemap_textureId != 0);
	assert(Cubemap_displayList != 0);
}

void Cubemap_Destroy()
{
	glDeleteLists(Cubemap_displayList,1);
	glDeleteTextures(1,&Cubemap_textureId);
	Cubemap_displayList = 0;
	Cubemap_textureId = 0;
}

static void Cubemap_s_Draw(void* textureId)
{
	GLUquadric* params = gluNewQuadric();
	glDisable(GL_FOG);   
	glDisable(GL_DEPTH);
	glDisable(GL_LIGHTING);
	glDisable(GL_BLEND);
	glDisable(GL_DEPTH_TEST);
	glColor4f(1,1,1,1);
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, (*(GLuint*)textureId));
	gluQuadricDrawStyle(params,GLU_FILL);
	gluQuadricOrientation(params,GLU_INSIDE);
    gluQuadricTexture(params,GL_TRUE);
	glRotatef(-90,1,0,0);
	gluSphere(params,512,64,64);
	gluDeleteQuadric(params);
	glEnable(GL_DEPTH_TEST);
}

void Cubemap_Draw(const Camera* camera)
{
	glPushMatrix();
	glTranslatef(camera->position.x,camera->position.y,camera->position.z);
	DisplayList_Draw(Cubemap_displayList);
	glPopMatrix();
}
